package com.lzh.tiktok.user.controller;

import com.lzh.common.utils.JwtUtils;
import com.lzh.tiktok.user.service.FollowService;
import com.lzh.tiktok.user.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lizhihang
 * @create 2023-01-17 15:12
 */
@RestController
@RequestMapping("/douyin/relation")
public class FollowController {
    @Autowired
    FollowService followService;

    /**
     * 获取关注列表
     */
    @GetMapping("/follow/list")
    public FollowResponseVo getAllFollowList(@RequestParam("user_id") String user_id,
                                             @RequestParam("token") String token) {
        if (user_id == null) {
            //什么时候userId为null?查看自己的页面信息的时候？
            user_id = JwtUtils.getUserId(token).toString();
        }
        List<UserVo> userVoList = followService.getAllFollowList(user_id, token);
        return FollowResponseVo.sucess(userVoList);
    }

    /**
     * 获取粉丝列表
     */
    @GetMapping("/follower/list")
    public FollowResponseVo getAllFollowerList(@RequestParam("user_id") String user_id,
                                               @RequestParam("token") String token) {
        if (user_id == null) {
            //什么时候userId为null?查看自己的页面信息的时候？
            user_id = JwtUtils.getUserId(token).toString();
        }
        List<UserVo> userVoList = followService.getAllFollowerList(user_id, token);
        return FollowResponseVo.sucess(userVoList);
    }

    /**
     * 关注或取关操作
     *
     * @param to_user_id  对方用户id
     * @param token       鉴权
     * @param action_type 1代表关注 2代表取关
     * @return
     */
    @PostMapping("/action")
    public CommonResponseVo followAction(@RequestParam("to_user_id") String to_user_id,
                                         @RequestParam("token") String token,
                                         @RequestParam("action_type") String action_type) {
        if (!("1".equals(action_type) || "2".equals(action_type))) {
            //action_type错误
            return CommonResponseVo.fail();
        }
        //拿到当前登录的用户id
        String loginUserId = JwtUtils.getUserId(token).toString();
        if (to_user_id.equals(loginUserId)) {
            //自己不能关注自己
            return CommonResponseVo.fail();
        }
        followService.doAction(to_user_id, loginUserId, action_type);
        return CommonResponseVo.success();
    }


    //获取当前登录用户的好友列表 并且带上与该用户的最新一条消息
    // /douyin/relation/friend/list/

    /**
     * 好友列表就是互相关注的人
     *
     * @param user_id
     * @param token
     * @return
     */
    @GetMapping("/friend/list")
    public FriendInfoResponseVo getFriendList(@RequestParam("user_id") String user_id,
                                              @RequestParam("token") String token) {

        if (user_id == null) {
            user_id = JwtUtils.getUserId(token).toString();
        }
        //获取好友列表
        FriendInfoResponseVo friendInfoResponseVo = followService.getFriendList(user_id, token);
        return friendInfoResponseVo;
    }

}
